﻿using PLCS.Application.Contracts.EquipmentApp.Dtos;
using PLCS.Application.Contracts.ErrorLibraryApp;
using PLCS.Application.Contracts.ErrorLibraryApp.Dtos;
using PLCS.Domain.ErrorLibraries;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace PLCS.Application.ErrorLibraryApp;

public class ErrorLibraryAppService : CrudAppService<ErrorLibrary, ErrorLibraryDto, Guid, PagedResultRequestDto, ErrorLibraryEditDto>, IErrorLibraryAppService
{
    public ErrorLibraryAppService(IRepository<ErrorLibrary, Guid> repository) : base(repository)
    {
    }

    public async Task<PagedResultDto<ErrorLibraryDto>> GetListBySearchAsync(PagedResultRequestDto input, string? code, string? kind, string? source)
    {
        await CheckGetListPolicyAsync();

        var query = await CreateFilteredQueryAsync(input);

        query = query.WhereIf(!string.IsNullOrEmpty(code), x => x.Code == code)
            .WhereIf(!string.IsNullOrEmpty(kind), x => x.Kind == kind)
            .WhereIf(!string.IsNullOrEmpty(source), x => x.Source == source);
        var totalCount = await AsyncExecuter.CountAsync(query);

        query = ApplyPaging(query, input);

        var entities = await AsyncExecuter.ToListAsync(query);
        var entityDtos = await MapToGetListOutputDtosAsync(entities);

        return new PagedResultDto<ErrorLibraryDto>(
            totalCount,
            entityDtos
        );
    }
}